//
//  DoorLockViewController.m
//  IntelligentHousehold
//
//  Created by wmwMAC on 17/6/27.
//  Copyright © 2017年 Cassie. All rights reserved.
//

#import "DoorLockViewController.h"


#define titleBtn_height 30
#define titleTF_height 50
@interface DoorLockViewController ()<UITextFieldDelegate>
{
    NSString *telStrl;
}

@property (nonatomic, strong) UITextField *tf;
@end

@implementation DoorLockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = BaseColor;
    
    self.navigationItem.title = @"请设置门锁号码";
    
    CGFloat btn_Weidth = [BasePublicClass weidthWithLabelText:@" 请设置开锁号码" labelHeight:20 labelFont:13];
    UIButton *titleBtn = [MyUtil createBtnFrame:CGRectMake(10, 0, btn_Weidth +30, titleBtn_height) title:@" 请设置开锁号码" bgImage:@"" selectBgImage:nil image:@"gth" target:self action:nil];
    [titleBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [titleBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [self.view addSubview:titleBtn];
    
    _tf = [MyUtil createTextFiledFrame:CGRectMake(0, titleBtn_height, kUIScreenWidth, titleTF_height) placeHolder:@"请输入门锁号码"];
    _tf.keyboardType = UIKeyboardTypeNumberPad;
    _tf.delegate = self;
    [self.view addSubview:_tf];
    
    UIButton *openBtn = [MyUtil createBtnFrame:CGRectMake(10, titleTF_height+titleBtn_height+50, kUIScreenWidth-20, titleTF_height-10) title:@"绑定" bgImage:@"ann" selectBgImage:nil image:nil target:self action:nil];
    [openBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [openBtn addTarget:self action:@selector(openBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:openBtn];
    
    
    
}
-(void)openBtnAction{
    telStrl = _tf.text;
    
//    BOOL istel = [BasePublicMethord isMobileNumber:telStrl];
//    if (istel) {
    
        [self setLockNumber];
        
//    }else{
//        [MBProgressHUD showText:@"门锁号码有误"];
//    }
    
}

-(void)setLockNumber{
    
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    NSString *url = [NSString stringWithFormat:LOCKNUMBER,RealmName];
    NSMutableDictionary *parameDic = [NSMutableDictionary dictionary];
    [parameDic setObject:saveDic[@"user_id"] forKey:@"user_id"];
    [parameDic setObject:saveDic[@"token"] forKey:@"token"];
    [parameDic setObject:telStrl forKey:@"locknumber"];
    [parameDic setObject:saveDic[@"tell"] forKey:@"tell"];
    
    
    [HttpRequest postWithURLString:url parameters:parameDic success:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        if ([[responseObject objectForKey:@"msg"] isEqualToString:@"success"]) {
            [MBProgressHUD showText:@"成功绑定"];
            
            [self mianLogin];
            
            
            
        }else{
            [MBProgressHUD showText:[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"msg"]]];
        }
        
        
    } failure:^(NSError *error) {
        
    }];
    
}

-(void)mianLogin{
    NSDictionary *saveDic = [SaveDataMethord readLoginData];
    
    NSString *url = [NSString stringWithFormat:MIANLOGIN,RealmName];
    NSMutableDictionary *parameDic = [NSMutableDictionary dictionary];
    [parameDic setObject:saveDic[@"tell"] forKey:@"tell"];
    
    [HttpRequest postWithURLString:url parameters:parameDic success:^(id responseObject) {
        
        [SaveDataMethord cleanloginData];//清除登录记录
        
        NSDictionary *dataDic = [NSDictionary dictionaryWithDictionary:responseObject];
        //存储信息
        [SaveDataMethord SaveLoginDataToLocationWithDic:dataDic.mutableCopy];
        
        
            [MBProgressHUD hideHUD];
            
            [self.navigationController popViewControllerAnimated:YES];

        
    } failure:^(NSError *error) {
        
    }];
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
